package com.hdy.orderforthree.pos;

import android.content.Context;

import com.dfire.cashsdk.DfPosSdk;
import com.dfire.cashsdk.DfResponse;
import com.dfire.cashsdk.exception.DfNetworkException;
import com.dfire.cashsdk.message.IDfMessageListener;
import com.dfire.cashsdk.model.DfLoginData;
import com.dfire.cashsdk.model.DfMenuData;
import com.dfire.cashsdk.model.DfOrderInfo;
import com.dfire.cashsdk.model.DfOrderRequestData;
import com.dfire.cashsdk.model.DfOrderResult;
import com.hdy.common.ObjectSharedPreference;
import com.hdy.common.ThreadPoolUtil;
import com.hdy.net.order.NetCallback;
import com.hdy.net.order.bean.LoginBean;
import com.hdy.net.order.bean.Response;
import com.hdy.orderforthree.BuildConfig;

import java.util.List;

public class DPos implements IPos<DfMenuData>{

    private static final String APP_KEY = "706af4753f1cdf92c71b5d6dbf864984";
    private static final String APP_SECRET = "1479ead0b9fabb9db260a1b1f4755798";

    private Context mContext;

    private LoginBean loginBean;
    private DfResponse<DfLoginData> loginRes;
    private DfResponse<DfOrderResult> orderResult;
    private DfResponse<List<DfMenuData>> goods;
    private String currentOrderId;
    private DfResponse<DfOrderInfo> orderInfo;

    /**
     * 初始化二维火pos
     * @param context
     */
    public DPos(Context context) {
        mContext = context;
        DfPosSdk.init(context, APP_KEY, APP_SECRET, BuildConfig.DEBUG);
        ObjectSharedPreference preference = new ObjectSharedPreference(mContext);
        loginBean = preference.get(LoginBean.class);
    }

    /**
     * 消息注册监听
     * @param dfMessageListener
     */
    public void registerMessageListener(IDfMessageListener dfMessageListener) {
        DfPosSdk.getInstance().registerMessageListener(dfMessageListener);
    }

    @Override
    public void login(LoginBean login, NetCallback callback) {
        ThreadPoolUtil.post(new Runnable() {
            @Override
            public void run() {
                try {
                    loginRes = DfPosSdk.getInstance().login(login.getIp(), login.getName(), login.getPassword());
                    if (loginRes.isSuccess()) {
                        saveLoginBean(login);
                    }
                    if (callback != null) {
                        Response response = new Response();
                        if (loginRes.isSuccess()) {
                            response.setCode("200");
                        } else {
                            response.setCode(loginRes.getCode());
                        }
                        response.setMessage(loginRes.getMessage());

                        callback.onNext(response);
                    }
                } catch (DfNetworkException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void order(Object orderRequestData, NetCallback callback) {
        ThreadPoolUtil.post(new Runnable() {
            @Override
            public void run() {
                try {
                    DfOrderRequestData requestData = (DfOrderRequestData) orderRequestData;
                    orderResult = DfPosSdk.getInstance().order(requestData);
                } catch (DfNetworkException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void getOrderInfo(String orderId, NetCallback callback) {
        ThreadPoolUtil.post(new Runnable() {
            @Override
            public void run() {
                try {
                    orderInfo = DfPosSdk.getInstance().getOrderInfo(orderId);
                } catch (DfNetworkException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void getAllMenus(NetCallback callback) {
        ThreadPoolUtil.post(new Runnable() {
            @Override
            public void run() {
                try {
                    goods = DfPosSdk.getInstance().getAllMenus();
                } catch (DfNetworkException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void addCart(DfMenuData dfMenuData) {

    }

    @Override
    public void removeCart(DfMenuData dfMenuData) {

    }

    @Override
    public void removeCartByDish(DfMenuData dfMenuData) {

    }

    @Override
    public LoginBean getLoginBean() {
        return loginBean;
    }

    @Override
    public void saveLoginBean(LoginBean bean) {
        loginBean = bean;
        ObjectSharedPreference preference = new ObjectSharedPreference(mContext);
        preference.save(bean);
    }
}
